<template>
  <PageWrapper>
    <BasicTable
      rowKey="id"
      :showTableSetting="true"
      @register="registerTable"
      :useSearchForm="true"
      :showIndexColumn="false"
      :beforeFetch="handleBeforeFetch"
      :rowSelection="rowSelection"
      :formConfig="{ labelWidth: 100, schemas: searchFormSchemas }"
    >
      <!--左上角按钮-->
      <template #tableTitle>
        <TableAction
          :actions="[
            {
              label: '新增',
              type: 'primary',
              size: 'middle',
              icon: 'ant-design:plus-outlined',
              onClick: handleAdd.bind(null),
              auth: ['admin===', 'sys:timer:save==='], // 根据权限控制是否显示: 无权限，不显示
            },
          ]"
          :dropDownActions="[
            {
              label: '重置',
              icon: 'ant-design:rest-outlined',
              popConfirm: {
                title: '确定重置吗？',
                confirm: handeReset.bind(null),
              },
              ifShow: () => {
                return checkedKeys.length > 0;
              },
              auth: ['admin', 'sys:timer:reset'], // 根据权限控制是否显示: 有权限，会显示
            },
          ]"
        >
          <template #more>
            <a-button style="margin-left: 8px">
              批量操作
              <DownOutlined />
            </a-button>
          </template>
        </TableAction>
      </template>
      <!--操作项-->
      <template #action="{ record }">
        <TableAction
          :stopButtonPropagation="true"
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
              auth: ['admin', 'sys:timer:update'], // 根据权限控制是否显示: 无权限，不显示
            },
          ]"
          :dropDownActions="[
            {
              label: '启动',
              auth: ['admin', 'sys:timer:start'],
              popConfirm: {
                title: '确定启动定时任务吗？',
                confirm: handleStart.bind(null, record),
              },
              ifShow: record.state === 2,
            },
            {
              label: '停止',
              auth: ['admin', 'sys:timer:stop'],
              popConfirm: {
                title: '确定停止定时任务吗？',
                confirm: handleStop.bind(null, record),
              },
              ifShow: record.state === 1,
            },
            {
              label: '重置',
              popConfirm: {
                title: '确定重置吗？',
                confirm: handeReset.bind(null, record),
              },
              auth: ['admin', 'sys:timer:reset'], // 根据权限控制是否显示: 有权限，会显示
            },
          ]"
        />
      </template> </BasicTable
    ><sysTimerAdd @register="registerAddModal" @success="handleSuccess"
  /></PageWrapper>
</template>

<script setup>
  import { ref, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { DownOutlined } from '@ant-design/icons-vue';
  import { sysTimerReset, sysTimerPage, sysTimerStart, sysTimerStop } from '/@/api/sys/timer';
  import sysTimerAdd from './add.vue';
  import { useModal } from '/@/components/Modal';
  import { message } from 'ant-design-vue';
  const columns = [
    // 列定义
    {
      title: '类名',
      dataIndex: 'actionClass',
    },
    {
      title: '默认表达式',
      dataIndex: 'cron',
    },
    {
      title: '当前表达式',
      dataIndex: 'redisCron',
    },
    {
      title: '状态',
      dataIndex: 'state',
      width: '10%',
      component: 'ApiDict',
      componentProps: {
        code: 'sys_timer_state',
      },
    },
    {
      title: '备注',
      dataIndex: 'remark',
    },
    {
      // flag: 'ACTION',
      title: '操作',
      key: 'ACTION',
      dataIndex: 'ACTION',
      slots: { customRender: 'action' },
    },
  ];
  // 搜索表单
  const searchFormSchemas = [
    {
      field: 'keywords',
      label: '关键字',
      component: 'Input',
      colProps: {
        xl: 6,
        xxl: 6,
      },
      componentProps: {
        placeholder: '请输入关键字',
        allowClear: true,
      },
    },
    {
      field: 'm_EQ_state',
      label: '状态',
      component: 'ApiDict',
      colProps: {
        xl: 6,
        xxl: 6,
      },
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        code: 'sys_timer_state',
      },
    },
  ];
  // 注册表格
  const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
    api: sysTimerPage,
    columns: columns,
  });
  // 选中行id集合
  const checkedKeys = ref([]);
  // 多选处理
  const rowSelection = {
    type: 'checkbox',
    onChange: (keys) => {
      checkedKeys.value = keys;
    },
  };
  const [registerAddModal, { openModal, setModalProps }] = useModal();
  // 打开添加表单
  const handleAdd = (record) => {
    openModal(true, {
      record: {
        parentId: record ? record.id : '',
      },
    });
    setModalProps({ title: '新增系统配置' });
  };
  // 打开编辑表单
  const handleEdit = (record) => {
    openModal(true, {
      record,
      isUpdate: true,
    });
    setModalProps({ title: '编辑系统配置' });
  };
  // 多选重置
  const handeReset = (record) => {
    const ids = [];
    if (record && record.id) {
      ids.push(record.id);
      checkedKeys.value = [];
    } else {
      ids.push(...unref(checkedKeys));
    }
    sysTimerReset({
      ids: ids,
    }).then(() => {
      message.success('重置成功');
      clearSelectedRowKeys();
      reload();
    });
  };
  const handleStart = (record) => {
    sysTimerStart({ id: record.id }).then(() => {
      message.success('启动成功');
      reload();
    });
  };
  const handleStop = (record) => {
    sysTimerStop({ id: record.id }).then(() => {
      message.success('停止成功');
      reload();
    });
  };
  // 添加、编辑成功回调
  const handleSuccess = () => {
    reload();
  };
  // 处理查询请求参数
  const handleBeforeFetch = (params) => {
    if (params) {
      Object.keys(params).forEach((key) => {
        if (params[key] == 9999999999) {
          // 重置全部自定义的值-不提交到后端
          delete params[key];
        }
      });
    }
    return params;
  };
</script>

<style lang="less" scoped></style>
